<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    <!--分页插件-->
    <script src="/after/js/laypage/laypage.js" charset="utf-8"></script>
    <script src="/after/js/layer/layer.js" charset="utf-8"></script>
    <link href="/after/js/laypage/skin/laypage.css" rel="stylesheet">


    <link rel="stylesheet" href="/after/css/bootstrap.min.css">
    <link rel="stylesheet" href="/after/css/laypage.css">
    <script type="text/javascript" src="/after/js/jquery-2.1.1.min.js"></script>
    <script type="text/javascript" src="/after/js/laypage/laypage.js"></script>
    <script type="text/javascript" src="/after/js/layer/layer.js"></script>
    <script type="text/javascript" src="/after/js/vue/vue.min.js"></script>
    <script src="/after/jquery.validation/1.14.0/jquery.validate.min.js"></script>
    <script src="/after/jquery.validation/1.14.0/messages_zh.min.js"></script>


    <style>
        input.error {
            border: 1px solid #E6594E;
        }
    </style>
</head>
<body>
<div id="app">
<div class="container">

    <form id="editUserForm">
        <br/>
        <div class="form-group">
            <label for="name">名称：</label>
            <input type="hidden"  id="ss"  :value="rs.id">
            <input type="text" class="form-control" id="name" name="name" :value="rs.name" placeholder="输入名称">
        </div>
        <div class="form-group">
            <label for="money">金额：</label>
            <input type="text" class="form-control" id="money" name="money" :value="rs.money" placeholder="输入金额">
        </div>
        <div class="form-group">
            <label for="conditio">满减条件：</label>
            <input type="text" class="form-control" id="conditio" name="conditio" :value="rs.conditio" placeholder="输入满减条件">
        </div>
        <div class="form-group">
            <label for="startTime">开始时间：</label>
            <input type="text" class="form-control" id="startTime" name="startTime" :value="rs.startTime" placeholder="输入开始时间">
        </div>
        <div class="form-group">
            <label for="endTime">结束时间：</label>
            <input type="text" class="form-control" id="endTime" name="endTime" :value="rs.endTime" placeholder="输入结束时间">
        </div>
        <div class="form-group">
            <label for="coun">总数：</label>
            <input type="text" class="form-control" id="coun" name="coun" :value="rs.coun" placeholder="输入总数">
        </div>
        <div class="form-group">
            <label for="remain">余数：</label>
            <input type="text" class="form-control" id="remain" name="remain" :value="rs.remain" placeholder="输入余数">
        </div>
        <div class="form-group">
            <button type="button" id="saveBtn" class="btn btn-success">提交</button>
            <button type="button" id="cancelBtn" class="btn btn-default">取消</button>
        </div>
    </form>
</div>
</div>
<script>


    var app = new Vue({
        el:'#app',
        data:{
            rs:[]
        }
    });

    function GetQueryString(sProp)
    {
        var re = new RegExp("[&,?]"+sProp + "=([^\\&]*)", "i");
        var a = re.exec(document.location.search);
        if (a == null)
            return "";
        return a[1];
    }

    var query=function (){
        $.ajax({
            type:"GET",
            dataType:"json",
            data:{
                id:GetQueryString("id")
            },
            url:"/czgDiscount/editp.do",
            success:function(msg){
                app.rs = msg.rs;
                app.rs.startTime = forlataTime(msg.rs.startTime);
                app.rs.endTime = forlataTime(msg.rs.endTime);
            },error:function(){
                alert("信息有误！请检查");
            }
        })
    }
    query();
    //沈敏时间
    var forlataTime = function (time) {
        //参数time为从后台取到的毫秒数
        dataTime = new Date(time);
        var y = dataTime.getFullYear();    //显示年;
        var m = dataTime.getMonth() + 1;    //显示月
        m = m < 10 ? ('0' + m) : m;
        var d = dataTime.getDate();     //显示日
        d = d < 10 ? ('0' + d) : d;
        var h = dataTime.getHours();    //显示小时
        h = h < 10 ? ('0' + h) : h;
        var minute = dataTime.getMinutes();     //显示分钟
        var s = dataTime.getSeconds();
        s = s < 10 ? ('0' + s) : s;
        minute = minute < 10 ? ('0' + minute) : minute;
        return y + '-' + m + '-' + d+' '+h+':'+minute+':'+s;     //返回最终时间y-m-d h-m
    };


    var editUser = function () {
        if (!check().form()) {
            return;
        }
        //检验时间大小
        var createTime=($("#startTime").val());
        var endTime=($("#endTime").val());
        var d1 = new Date(createTime.replace(/\-/g, "\/"));
        var d2 = new Date(endTime.replace(/\-/g, "\/"));
        if(d2-d1<=0) {
            layer.msg("开始时间不能大于或等于结束时间");
            return false;
        }
        //检验金额
        var money= parseInt(document.getElementById("money").value);
        var conditio = parseInt (document.getElementById("conditio").value);
        if(money>conditio){
            layer.msg("满减金额有误！");
            return;
        }
        //检验总数余数
        var coun= parseInt(document.getElementById("coun").value);
        var remain = parseInt (document.getElementById("remain").value);
        if(coun<remain){
            layer.msg("总数余数有误！");
            return;
        }


        $.ajax({
            type: "POST",
            dataType: "json",
            url: "/czgDiscount/edit.do",
            data:{
                "id": $("#ss").val(),
                "name": $("#name").val(),
                "money":$("#money").val(),
                "conditio": $("#conditio").val(),
                "startTime":new Date($("#startTime").val()),
                "endTime":new Date($("#endTime").val()) ,
                "coun":$("#coun").val(),
                "remain": $("#remain").val()
            },
            success: function (msg) {
                $('#cancelBtn').click();
            },error:function(){
                layer.msg("信息有误！请检查");
            }
        });
    }

    $('#saveBtn').on('click', function () {
        editUser();
    });

    $('#cancelBtn').on('click', function () {
        var index = parent.layer.getFrameIndex(window.name);
        parent.getUserPageList();
        parent.layer.close(index);
    });


    //校验字段是否正确
    function check(){
        /*返回一个validate对象，这个对象有一个form方法，返回的是是否通过验证*/
        return $("#editUserForm").validate({
            rules:{
                name:{
                    required:true
                },
                money:{
                    required:true
                },
                conditio:{
                    required:true
                },
                startTime:{
                    required:true
                },
                endTime:{
                    required:true
                },
                coun:{
                    required:true
                },
                remain:{
                    required:true
                }
            },
            messages:{
                name:{
                    required:""
                },
                money:{
                    required:""
                },
                conditio:{
                    required:""
                },
                startTime:{
                    required:""
                },
                endTime:{
                    required:""
                },
                coun:{
                    required:""
                },
                remain:{
                    required:""
                }
            }
        });
    }

</script>

</body>